/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.employee.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.employee.dao.StoreBonusSettingCardDao;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingCard;
import com.chlitina.store.modules.employee.entity.StoreBonusSettingTemplateCard;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 新开卡或卡充值提成设定Service
 * @author liujiang
 * @version 2013-11-13
 */
@Component
@Transactional(readOnly = true)
public class StoreBonusSettingCardService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreBonusSettingCardService.class);
	
	@Autowired
	private StoreBonusSettingCardDao storeBonusSettingCardDao;
	@Autowired
	private StoreBonusSettingTemplateCardService storeBonusSettingTemplateCardService;
	
	public StoreBonusSettingCard get(Long id) {
		return storeBonusSettingCardDao.findOne(id);
	}
	
	public List<Long> getExistTemplateId(Long employeeId, int type) {
		return storeBonusSettingCardDao.getExistTemplateId(employeeId, type);
	}
	
	public Page<StoreBonusSettingCard> find(Page<StoreBonusSettingCard> page, Long employeeId) {
		DetachedCriteria dc = storeBonusSettingCardDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (employeeId != null){
			dc.add(Restrictions.eq("employee.id", employeeId));
		}
		dc.addOrder(Order.desc("id"));
		return storeBonusSettingCardDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreBonusSettingCard storeBonusSettingCard) {
		storeBonusSettingCardDao.save(storeBonusSettingCard);
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify) {
		List<StoreBonusSettingTemplateCard> list = storeBonusSettingTemplateCardService.getByStore();
		List<StoreBonusSettingCard> existList = getByEmployee(employee);
		for (StoreBonusSettingTemplateCard stc : list) {
			StoreBonusSettingCard same = null;
			for (StoreBonusSettingCard sc : existList) {
				if(stc.getCard_template_type() == sc.getCard_template_type()
						&& stc.getCard_template_id() == sc.getCard_template_id()) {
					same = sc;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stc.getBonus_type());
					same.setBonus_rate(stc.getBonus_rate());
					same.setFixed_money(stc.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingCard storeBonusSettingCard = new StoreBonusSettingCard();
				storeBonusSettingCard.setStore(stc.getStore());
				storeBonusSettingCard.setEmployee(employee);
				storeBonusSettingCard.setCard_template_type(stc.getCard_template_type());
				storeBonusSettingCard.setCard_template_id(stc.getCard_template_id());
				storeBonusSettingCard.setBonus_type(stc.getBonus_type());
				storeBonusSettingCard.setBonus_rate(stc.getBonus_rate());
				storeBonusSettingCard.setFixed_money(stc.getFixed_money());
				save(storeBonusSettingCard);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void copy(Employee employee, int ifModify, Employee from) {
		List<StoreBonusSettingCard> list = getByEmployee(from);
		List<StoreBonusSettingCard> existList = getByEmployee(employee);
		for (StoreBonusSettingCard stc : list) {
			StoreBonusSettingCard same = null;
			for (StoreBonusSettingCard sc : existList) {
				if(stc.getCard_template_type() == sc.getCard_template_type()
						&& stc.getCard_template_id() == sc.getCard_template_id()) {
					same = sc;
					break;
				}
			}
			if(same != null) {
				if(ifModify == 1) {
					same.setBonus_type(stc.getBonus_type());
					same.setBonus_rate(stc.getBonus_rate());
					same.setFixed_money(stc.getFixed_money());
					save(same);
				}
			} else {
				StoreBonusSettingCard storeBonusSettingCard = new StoreBonusSettingCard();
				storeBonusSettingCard.setStore(stc.getStore());
				storeBonusSettingCard.setEmployee(employee);
				storeBonusSettingCard.setCard_template_type(stc.getCard_template_type());
				storeBonusSettingCard.setCard_template_id(stc.getCard_template_id());
				storeBonusSettingCard.setBonus_type(stc.getBonus_type());
				storeBonusSettingCard.setBonus_rate(stc.getBonus_rate());
				storeBonusSettingCard.setFixed_money(stc.getFixed_money());
				save(storeBonusSettingCard);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreBonusSettingCard storeBonusSettingCard) {
		storeBonusSettingCardDao.delete(storeBonusSettingCard);
	}
	
	@Transactional(readOnly = false)
	public List<StoreBonusSettingCard> getByEmployee(Employee employee) {
		DetachedCriteria dc = storeBonusSettingCardDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		dc.add(Restrictions.eq("employee", employee));
		return storeBonusSettingCardDao.find(dc);
	}
}
